package com.example.jwt.main;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.cmsr.common.util.JWTUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: xiaohonggong
 * @date: Created in 2024/1/24 下午5:57
 */

@SpringBootApplication
@EnableCreateCacheAnnotation
public class JwtApplication implements ApplicationRunner {

    @CreateCache(name = "custom:auth:update_info_after_login : ")
    private Cache<String, String> orderCache;


    public static void main(String[] args) {
        SpringApplication.run(JwtApplication.class, args);
    }

    public static List<String[]> readCSV(String filePath) {
        List<String[]> dataList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] data = line.split(",");
                dataList.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("第一页");
        List<String[]> csvData = readCSV("/data/token/http_msg.csv");
        //List<String[]> csvData = readCSV("src/main/resources/http_msg.csv");
        sheet.setColumnWidth(0, 25 * 256);
        sheet.setColumnWidth(1, 25 * 256);
        sheet.setColumnWidth(2, 130 * 256);


        for (int i = 0; i < csvData.size(); i++) {

            System.out.println("*******第" + i + "行处理开始******");
            String customerId = csvData.get(i)[4];
            String tenantId = csvData.get(i)[5];
            String jrdtoken = csvData.get(i)[8];
            System.out.println("----读取到第" + i + "行customer_id:" + customerId + "----");
            System.out.println("----读取到第" + i + "行optenant_id:" + tenantId + "----");
            System.out.println("----读取到第" + i + "行Token:" + jrdtoken + "----");
            //String token = JWTUtil.buildJWT(customerId, tenantId);
            //System.out.println("----加密第" + i + "行数据,获取token:" + token + "----");
            System.out.println("读取csv数据与jwt加密后是否相等:" + jrdtoken.equals(jrdtoken));
            Row row = sheet.createRow(i);
            Cell cell = row.createCell(0);
            cell.setCellValue(customerId);

            Cell cell1 = row.createCell(1);
            cell1.setCellValue(tenantId);

            Cell cell2 = row.createCell(2);
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setWrapText(true);
            cell2.setCellStyle(cellStyle);
            cell2.setCellValue(jrdtoken);

            String key = customerId;
            //验证数据是否存在
            String s = orderCache.get(key);
            if (StringUtils.isNotBlank(s)) {
                System.out.println("--------redis中存在key为:" + key + "的数据:" + s + "---------");
            } else {
                System.out.println("--------redis中不存在key为:" + key + "的数据---------");
            }
            //存数据
            System.out.println("--------存入key为:" + key + "的数据:" + jrdtoken + "---------");
            orderCache.put(key, jrdtoken, 86400*3, TimeUnit.SECONDS);
            //缓存24小时
            System.out.println("--------查询key为:" + key + "的数据是否存在---------");
            s = orderCache.get(key);
            System.out.println("--------查询key为:" + key + "的数据:" + s + "---------");
            System.out.println("*******第" + i + "行处理完毕*******");
        }
        writeToFile("C:\\Users\\马永兴\\Desktop\\token\\token.xls", workbook);
        //writeToFile("src/main/resources/token.xls", workbook);

    }

    public void writeToFile(String filePath, Workbook workbook) {
        File file = new File(filePath);
        //将文件保存到指定的位置
        try {
            workbook.write(new FileOutputStream(file));
            System.out.println("写入成功");
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


